package com.zzt.questionbankquestion.service.impl;

import com.zzt.questionbankquestion.bean.Difficulty;
import com.zzt.questionbankquestion.bean.dto.DifficultyDTO;
import com.zzt.questionbankquestion.mapper.DifficultyConverter;
import com.zzt.questionbankquestion.repository.DifficultyRepository;
import com.zzt.questionbankquestion.service.DifficultyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class DifficultyServiceImpl implements DifficultyService {
    @Autowired
    private DifficultyConverter difficultyConverter;

    @Autowired
    private DifficultyRepository difficultyRepository;

    @Override
    public Boolean addDifficulty(DifficultyDTO difficultyDTO) {
        if(difficultyDTO.getDifficultyName()==null){
            System.out.println("难度名称不能为空");
            return false;
        }
        if (difficultyRepository.findByDifficultyName(difficultyDTO.getDifficultyName())==null){
            Difficulty difficulty=difficultyConverter.dtoToDo(difficultyDTO);
            int id=difficultyRepository.save(difficulty).getId();
            System.out.println(id);
            return true;
        }
        else{
            System.out.println("难度已存在");
            return false;
        }
    }

    @Override
    public Boolean editDifficulty(DifficultyDTO difficultyDTO){
        if(difficultyDTO.getDifficultyName()==null || difficultyDTO.getId()==0){
            System.out.println("参数不能为空");
            return false;
        }
        if(!difficultyRepository.findById(difficultyDTO.getId()).isPresent()){
            System.out.println("没有这个对象");
            return false;
        }else {
            Difficulty difficulty=difficultyConverter.dtoToDo(difficultyDTO);
            difficultyRepository.save(difficulty);
            System.out.println("修改成功");
            return true;
        }
    }

    @Override
    public Boolean deleteDifficulty(DifficultyDTO difficultyDTO){
        if(difficultyDTO.getId()==0){
            System.out.println("参数不能为空");
            return false;
        }
        if(!difficultyRepository.findById(difficultyDTO.getId()).isPresent()){
            System.out.println("没有这个对象");
            return false;
        }else {
            difficultyRepository.deleteById(difficultyDTO.getId());
            return true;
        }
    }

    @Override
    public DifficultyDTO findByName(String name){
        Difficulty entity=difficultyRepository.findByDifficultyName(name);
        DifficultyDTO dto=difficultyConverter.doToDto(entity);
        return dto;
    }

    @Override
    public List<DifficultyDTO> getAllList(){

        List<Difficulty> difficulties=(List<Difficulty>)difficultyRepository.findAll();
        List<DifficultyDTO> difficultyDTOS=difficultyConverter.doListToDtoList(difficulties);
        return difficultyDTOS;
    }
}
